<template>
  <BaseLayout>
    <template #content>
      <el-table
        border
        v-loading="tableLoading"
        :data="tableData"
        :stripe="true"
        :header-cell-style="() => ({ background: '#F8F8F8' })"
      >
        <el-table-column
          prop="id"
          label="角色编号"
          min-width="150"
          align="center"
        />

        <el-table-column
          prop="name"
          label="角色名称"
          min-width="150"
          align="center"
        />

        <el-table-column
          prop="code"
          label="角色标识"
          min-width="150"
          align="center"
        />

        <el-table-column
          prop="sort"
          label="显示顺序"
          min-width="120"
          align="center"
        />

        <el-table-column
          prop="status"
          label="状态"
          min-width="100"
          align="center"
        >
          <template #default="scope">
            <el-tag
              :type="scope.row.status === 0 ? 'success' : 'danger'"
              disable-transitions
            >
              {{ scope.row.status === 0 ? '正常' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          prop="type"
          label="角色类型"
          min-width="120"
          align="center"
        >
          <template #default="scope">
            {{ scope.row.type === 1 ? '内置角色' : '自定义角色' }}
          </template>
        </el-table-column>

        <el-table-column
          prop="remark"
          label="备注"
          min-width="150"
          align="center"
        />

        <el-table-column
          prop="createTime"
          label="创建时间"
          min-width="180"
          :formatter="formatDate"
          align="center"
        />
      </el-table>
    </template>
  </BaseLayout>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { getRolePages } from '@/api/role'
import dayjs from 'dayjs'
import BaseLayout from '@/layout/baseLayout/index.vue'

const tableData = ref([])
const tableLoading = ref(true)

onMounted(async () => {
  const data = await getRolePages()
  tableData.value = data?.list
  tableLoading.value = false
})

const formatDate = (_row, _column, cellValue) => {
  if (!cellValue) {
    return '-'
  }

  return typeof cellValue === 'number' ? dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss') : '-'
}
</script>

<style scoped>
/* 表头背景颜色 */
.el-table th {
  background-color: #f5f7fa; /* 设置表头背景颜色 */
  color: #333; /* 设置表头文字颜色 */
}
</style>
